package com.goodving.guli.bt.model;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateFormat;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.From;
import com.activeandroid.query.Select;
import com.goodving.guli.bt.dataprocess.SportDay;
import com.goodving.guli.bt.eventbus.EventWeightSyncedServer;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.utils.aE;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.model.SyncResult;
import com.goodving.guli.model.uploadhelper.SyncerUpload;
import com.goodving.guli.model.weightsyncupload.SyncerWeightDelete;
import com.goodving.guli.model.weightsyncupload.SyncerWeightUpload;
import com.goodving.guli.server.WebAPI;
import com.goodving.guli.server.WebResponse;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.com.smartdevices.bracelet.config.C0886a;
import cz.msebera.android.httpclient.Header;
import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/1.
 */
public class WeightInfoManager { //ah
    private static final String TAG = "WeightInfoManager";
    private static WeightInfoManager weightInfoManager;

    private WeightInfoManager() {
    }

    public static WeightInfoManager getInstance() { // m15728a
        if (weightInfoManager == null) {
            weightInfoManager = new WeightInfoManager();
        }
        return weightInfoManager;
    }

    private List<WeightInfo> m15730a(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str).getJSONObject("data");
            List<WeightInfo> arrayList = new ArrayList(jSONObject.getInt("total"));
            JSONArray jSONArray = jSONObject.getJSONArray("list");
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                WeightInfo weightInfo = new WeightInfo();
                weightInfo.uid = jSONObject2.getInt("fuid");
                weightInfo.weight = Float.valueOf(jSONObject2.getString("wt")).floatValue();
                weightInfo.timestamp = jSONObject2.getLong("ts") * 1000;
                arrayList.add(weightInfo);
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private long[] m15731b(Calendar calendar) {
        long[] jArr = new long[2];
        Calendar calendar2 = (Calendar) calendar.clone();
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        jArr[0] = calendar2.getTimeInMillis();
        GuliLog.tracePrintInfo(TAG, "from time :" + new SimpleDateFormat("HH:mm:ss").format(Long.valueOf(calendar2.getTimeInMillis())));
        calendar2.set(Calendar.HOUR_OF_DAY, 23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        jArr[1] = calendar2.getTimeInMillis();
        GuliLog.tracePrintInfo(TAG, "to time :" + new SimpleDateFormat("HH:mm:ss").format(Long.valueOf(calendar2.getTimeInMillis())));
        return jArr;
    }

    private void deleteInfosMark(List<WeightInfo> list) { // m15732e
        GuliLog.m12370d(TAG, "Delete Infos Mark : " + list.size());
        ActiveAndroid.beginTransaction(); //C2112a.m16249d();
        try {
            for (WeightInfo i : list) {
                deleteInfoMark(i);
            }
            ActiveAndroid.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ActiveAndroid.endTransaction(); //C2112a.m16250e();
        }
    }

    private String m15733f(List<WeightInfo> list) {
        String jSONArray;
        String str = C3657a.f18328f;
        try {
            JSONArray jSONArray2 = new JSONArray();
            for (WeightInfo weightInfo : list) {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("fuid", weightInfo.uid);
                jSONObject.put("wt", Float.valueOf(weightInfo.weight));
                jSONObject.put("ts", weightInfo.timestamp / 1000);
                if (!TextUtils.isEmpty(weightInfo.deviceid)) {
                    jSONObject.put("did", weightInfo.deviceid);
                }
                jSONArray2.put(jSONObject);
            }
            jSONArray = jSONArray2.toString();
        } catch (Exception e) {
            e.printStackTrace();
            jSONArray = str;
        }
        GuliLog.tracePrintInfo(TAG, "Sync WeightInfos : " + jSONArray);
        return jSONArray;
    }

    public static boolean m15734f(WeightInfo weightInfo) {
        return weightInfo.synced == 2;
    }

    private String m15735g(List<WeightInfo> list) {
        String jSONArray;
        String str = C3657a.f18328f;
        try {
            JSONArray jSONArray2 = new JSONArray();
            for (WeightInfo weightInfo : list) {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("ts", weightInfo.timestamp / 1000);
                jSONObject.put("fuid", weightInfo.uid);
                jSONArray2.put(jSONObject);
            }
            jSONArray = jSONArray2.toString();
        } catch (Exception e) {
            e.printStackTrace();
            jSONArray = str;
        }
        GuliLog.tracePrintInfo(TAG, "Sync Deleted WeightInfos : " + jSONArray);
        return jSONArray;
    }

    private void m15736g(int i) {
        GuliLog.tracePrintInfo(TAG, "Delete Infos Mark UID : " + i);
        deleteInfosMark(getInfosByUid(i));
    }

    public static boolean m15737g(WeightInfo weightInfo) {
        return weightInfo.synced == -2;
    }

    public static boolean m15738h(WeightInfo weightInfo) {
        return weightInfo.synced == 2 || weightInfo.synced == -2;
    }

    private void deleteInfoMark(WeightInfo weightInfo) { // m15739i
        GuliLog.m12370d(TAG, "Delete Info Mark : " + weightInfo);
        if (weightInfo.synced == 2) {
            weightInfo.synced = -2;
        } else {
            weightInfo.synced = -1;
        }
        m15740j(weightInfo);
    }

    private void m15740j(WeightInfo weightInfo) {
        m15764b(weightInfo, false);
    }

    private From getInfoNeedToDelete() { // m15741k
        return new Select().from(WeightInfo.class).where("Synced=?", Integer.valueOf(-1)); //C2118e().m16298a(WeightInfo.class).m16267a("Synced=?", Integer.valueOf(-1));
    }

    private From getInfoNeedToSync() { // m15742l
        return new Select().from(WeightInfo.class).where("Synced=?", Integer.valueOf(0)).and("UserId<>?", Integer.valueOf(0));
    }

    public WeightInfo m15743a(int i, int i2) {
        Object[] objArr = new Object[]{Integer.valueOf(i)};
        WeightInfo weightInfo = new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", objArr).offset(i2).orderBy("Timestamp DESC").executeSingle();
        //WeightInfo weightInfo = (WeightInfo) new C2118e().m16298a(WeightInfo.class).m16267a("Synced>?", Integer.valueOf(-1)).m16273b("UserId=?", objArr).m16271b(i2).m16289g("Timestamp DESC").m16285e();
        GuliLog.tracePrintInfo(TAG, "Get Info : " + i + ", " + i2 + ", " + weightInfo);
        return weightInfo;
    }

    public WeightInfo m15744a(int i, long j) {
        return (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", Integer.valueOf(i)).and("Timestamp<?", Long.valueOf(j)).orderBy("Timestamp DESC").executeSingle();
    }

    public WeightInfo m15745a(long j, boolean z) {
        if (!z) {
            return m15760b(j);
        }
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("TimeStamp=?", Long.valueOf(j)).executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Info : " + DateFormat.format("yyyy-MM-dd HH:mm:ss", j) + ", " + weightInfo);
        return weightInfo;
    }

    public List<WeightInfo> getInfosByUid(int i) { // m15746a
        Object[] objArr = new Object[]{Integer.valueOf(i)};
        List<WeightInfo> d = new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", objArr).orderBy("Timestamp DESC").execute();
        GuliLog.tracePrintInfo(TAG, "Get Infos : " + i + ", " + d.size());
        return d;
    }

    public List<WeightInfo> m15747a(int i, Calendar calendar) {
        long[] b = m15731b(calendar);
        Object[] objArr = new Object[]{Integer.valueOf(i)};
        objArr = new Object[]{Long.valueOf(b[0]), Long.valueOf(b[1])};
        List<WeightInfo> d = new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", objArr).and("Timestamp>=? AND Timestamp<=?", objArr).orderBy("Timestamp DESC").execute();
        GuliLog.tracePrintInfo(TAG, "Get Infos : " + i + " " + DateFormat.format("yyyy-MM-dd hh:mm:ss", calendar) + ", " + d.size());
        for (WeightInfo weightInfo : d) {
            GuliLog.tracePrintInfo(TAG, "Get Info : " + weightInfo);
        }
        return d;
    }

    public List<WeightInfo> m15748a(Calendar calendar) {
        long[] b = m15731b(calendar);
        Object[] objArr = new Object[]{Long.valueOf(b[0]), Long.valueOf(b[1])};
        List<WeightInfo> d = new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("Timestamp>=? AND Timestamp<=?", objArr).orderBy("Timestamp DESC").execute();
        GuliLog.tracePrintInfo(TAG, "Get Infos : " + DateFormat.format("yyyy-MM-dd hh:mm:ss", calendar) + ", " + d.size());
        for (WeightInfo weightInfo : d) {
            GuliLog.tracePrintInfo(TAG, "Get Info : " + weightInfo);
        }
        return d;
    }

    public void m15749a(WeightInfo weightInfo) {
        GuliLog.m12370d(TAG, "Add Info : " + weightInfo);
        weightInfo.save();
    }

    public void m15750a(WeightInfo weightInfo, int i) {
        GuliLog.m12370d(TAG, "Update Info : " + weightInfo + " New UID : " + i);
        WeightInfo weightInfo2 = new WeightInfo(weightInfo);
        weightInfo2.uid = i;
        if (weightInfo2.synced == 1) {
            weightInfo2.synced = 0;
        }
        m15749a(weightInfo2);
        if (weightInfo.synced == 1) {
            deleteInfoMark(weightInfo);
        } else {
            deleteInfoLocal(weightInfo);
        }
    }

    public void m15751a(WeightInfo weightInfo, boolean z) {
        GuliLog.m12370d(TAG, "Add Info : " + weightInfo + ", Uncertain : " + z);
        if (z) {
            weightInfo.synced = 2;
        }
        weightInfo.save();
    }

    public boolean m15752a(long j) {
        return (new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("Timestamp=?", Long.valueOf(j)).executeSingle()) != null;
    }

    public boolean syncFromServer(Context context) { // m15753a
        boolean z = !Keeper.readScaleSyncedWeightInfosFromServer();
        GuliLog.tracePrintInfo(TAG, "Sync with Server 1, from server : " + z);
        return z ? syncWeightInfosToLocal(context, new al(this, context)) : syncWithServer(context);
    }

    class al implements aE {
        final /* synthetic */ Context f11347a;
        final /* synthetic */ WeightInfoManager f11348b;

        al(WeightInfoManager ahVar, Context context) {
            this.f11348b = ahVar;
            this.f11347a = context;
        }

        public void onSuccess() {
            EventBus.getDefault().post(new EventWeightSyncedServer(true));
            this.f11348b.syncWithServer(this.f11347a);
        }

        public void onFailure() {
        }
    }

    class SyncWeightInfosToLocalHandler extends AsyncHttpResponseHandler {
        final /* synthetic */ SyncResult f11335a;
        final /* synthetic */ Context f11336b;
        final /* synthetic */ aE f11337c;
        final /* synthetic */ WeightInfoManager f11338d;

        SyncWeightInfosToLocalHandler(WeightInfoManager ahVar, SyncResult syncResult, Context context, aE aEVar) {
            this.f11338d = ahVar;
            this.f11335a = syncResult;
            this.f11336b = context;
            this.f11337c = aEVar;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            GuliLog.m12370d("WeightInfoManager", "Sync WeightInfos To Local Failure!!");
            if (bArr != null) {
                GuliLog.tracePrintInfo("WeightInfoManager", "Response : " + new String(bArr));
            }
            this.f11335a.result = false;
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            GuliLog.m12370d("WeightInfoManager", "Sync WeightInfos To Local Success!!");
            if (bArr != null) {
                String str = new String(bArr);
                GuliLog.tracePrintInfo("WeightInfoManager", "Response : " + str);
                WebResponse a = WebResponse.getWebResponse(this.f11336b, str);
                if (a.m11659c()) {
                    GuliLog.m12370d("WeightInfoManager", "Result OK!!");
                    this.f11335a.result = true;
                    List<WeightInfo> a2 = this.f11338d.m15730a(str);
                    if (a2 != null && !a2.isEmpty()) {
                        for (WeightInfo weightInfo : a2) {
                            weightInfo.synced = 1;
                        }
                        if (this.f11338d.m15756a((List) a2)) {
                            Keeper.keepScaleSyncedWeightInfosFromServer(true);
                            if (this.f11337c != null) {
                                this.f11337c.onSuccess();
                            }
                        }
                    }
                } else if (a.m11657a()) {
                    GuliLog.m12370d("WeightInfoManager", "Result Empty OK!!");
                    Keeper.keepScaleSyncedWeightInfosFromServer(true);
                    if (this.f11337c != null) {
                        this.f11337c.onSuccess();
                    }
                }
            }
        }
    }

    public boolean syncWeightInfosToLocal(Context context, aE aEVar) { // m15754a
        GuliLog.tracePrintInfo(TAG, "Sync WeightInfos To Local!!");
        SyncResult syncResult = new SyncResult();
        WebAPI.m11599a(C0886a.m9071f(context), -1, -1, -1, true, new SyncWeightInfosToLocalHandler(this, syncResult, context, aEVar));
        return syncResult.result; //TBD
    }

    class SyncWeightInfosToServerHandler extends AsyncHttpResponseHandler {
        final /* synthetic */ Context f11339a;
        final /* synthetic */ SyncResult f11340b;
        final /* synthetic */ List<WeightInfo> f11341c;
        final /* synthetic */ WeightInfoManager f11342d;

        SyncWeightInfosToServerHandler(WeightInfoManager ahVar, Context context, SyncResult syncResult, List list) {
            this.f11342d = ahVar;
            this.f11339a = context;
            this.f11340b = syncResult;
            this.f11341c = list;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            GuliLog.m12370d("WeightInfoManager", "Sync WeightInfos To Server Failure!!");
            if (bArr != null) {
                GuliLog.tracePrintInfo("WeightInfoManager", "Response : " + new String(bArr));
            }
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            GuliLog.m12370d("WeightInfoManager", "Sync WeightInfos To Server Success!!");
            if (bArr != null) {
                String str = new String(bArr);
                GuliLog.tracePrintInfo("WeightInfoManager", "Response : " + str);
                if (WebResponse.getWebResponse(this.f11339a, str).m11659c()) {
                    GuliLog.m12370d("WeightInfoManager", "Result OK!!");
                    this.f11340b.result = true;
                    for (WeightInfo weightInfo : this.f11341c) {
                        weightInfo.synced = 1;
                    }
                    this.f11342d.updateInfos(this.f11341c);
                }
            }
        }
    }


    public boolean m15755a(Context context, List<WeightInfo> list) {
        GuliLog.tracePrintInfo(TAG, "Sync WeightInfos To Server!!");
        if (list == null || list.isEmpty()) {
            return true;
        }
        String f = m15733f((List) list);
        SyncResult syncResult = new SyncResult();
        // TBD sync to server
        WebAPI.syncWeightInfosToServer(C0886a.m9071f(context), f, new SyncWeightInfosToServerHandler(this, context, syncResult, list));
        return syncResult.result;
    }

    public boolean m15756a(List<WeightInfo> list) {
        return m15757a((List) list, false);
    }


    private boolean m15757a(List<WeightInfo> list, boolean flag) {

        boolean result = false;
        GuliLog.m12370d(TAG, "Add Infos : " + ", Uncertain : " + flag + list.size());
        ActiveAndroid.beginTransaction(); //C2112a.m16249d();
        try {
            for (WeightInfo i : list) {
                m15751a(i, flag);
            }
            ActiveAndroid.setTransactionSuccessful();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ActiveAndroid.endTransaction(); //C2112a.m16250e();
        }
        return result;
    }

    public int m15758b(int i) {
//        return new Select(C2111d.f11905a).from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", Integer.valueOf(i)).m16288g();
        return new Select(Table.DEFAULT_ID_NAME).from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", Integer.valueOf(i)).count();
    }

    public WeightInfo m15759b(int i, Calendar calendar) {
        long[] b = m15731b(calendar);
        Object[] objArr = new Object[]{Integer.valueOf(i)};
        objArr = new Object[]{Long.valueOf(b[0]), Long.valueOf(b[1])};
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", objArr).and("Timestamp>=? AND Timestamp<=?", objArr).orderBy("Timestamp DESC").executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Latest Info : " + i + ", " + DateFormat.format("yyyy-MM-dd hh:mm:ss", calendar) + ", " + weightInfo);
        return weightInfo;
    }

    public WeightInfo m15760b(long j) {
        Object[] objArr = new Object[]{Long.valueOf(j)};
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("TimeStamp=?", objArr).executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Info : " + DateFormat.format("yyyy-MM-dd HH:mm:ss", j) + ", " + weightInfo);
        return weightInfo;
    }

    public List<WeightInfo> m15761b(int i, int i2) {
        if (i2 <= 0) {
            throw new IllegalArgumentException();
        }
        return new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", Integer.valueOf(i)).orderBy("Timestamp DESC").limit(i2).execute();
    }

    public void deleteAllInfos() { // m15762b
        GuliLog.m12370d(TAG, "Delete All Infos!!");
        //new C2114a().m16254a(WeightInfo.class).execute();
        new Delete().from(WeightInfo.class).execute();
    }

    public void m15763b(WeightInfo weightInfo) {
        m15751a(weightInfo, true);
    }

    public void m15764b(WeightInfo weightInfo, boolean z) {
        GuliLog.m12370d(TAG, "Update Info : " + weightInfo + ", markUpdated : " + z);
        if (z && weightInfo.synced == 1) {
            weightInfo.synced = 0;
        }
        weightInfo.save();
    }

    public void m15765b(List<WeightInfo> list) {
        m15757a((List) list, true);
    }

    public boolean syncWithServer(Context context) {
        // delete at most 3 times for each time
        int i = 3;
        LoginData f = C0886a.m9071f(context);
        boolean i2 = true;
        // sync at most 3 times for each time
        int i3 = 3;
        do {
            int g = getCountInfoNeedToSync();
            GuliLog.m12370d(TAG, "Sync with Server 2, to server : " + g);
            if (g > 0) {
                SyncerUpload syncerWeightUpload = new SyncerWeightUpload(context, f, Integer.valueOf(100));
                syncerWeightUpload.work();
                i2 &= syncerWeightUpload.isSuccessed();
            }
            GuliLog.m12370d(TAG, "Sync with Server 2, to server : " + i2);
            i3--;
            if (g <= 0) {
                break;
            }
        } while (i3 > 0);
        do {
            i3 = getCountInfoNeedToDelete();
            GuliLog.m12370d(TAG, "Sync with Server 3, delete from server : " + i3);
            if (i3 > 0) {
                SyncerUpload syncerWeightDelete = new SyncerWeightDelete(context, f, Integer.valueOf(C4220a.aB));
                syncerWeightDelete.work();
                i2 &= syncerWeightDelete.isSuccessed();
            }
            GuliLog.m12370d(TAG, "Sync with Server 3, delete from server : " + i2);
            i--;
            if (i3 <= 0) {
                break;
            }
        } while (i > 0);
        return i2;
    }

    class SyncDeletedWeightInfosToServerHandler extends AsyncHttpResponseHandler {
        final /* synthetic */ Context f11343a;
        final /* synthetic */ SyncResult f11344b;
        final /* synthetic */ List f11345c;
        final /* synthetic */ WeightInfoManager f11346d;

        SyncDeletedWeightInfosToServerHandler(WeightInfoManager ahVar, Context context, SyncResult syncResult, List list) {
            this.f11346d = ahVar;
            this.f11343a = context;
            this.f11344b = syncResult;
            this.f11345c = list;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            GuliLog.m12370d("WeightInfoManager", "Sync Deleted WeightInfos To Server Failure!!");
            if (bArr != null) {
                GuliLog.tracePrintInfo("WeightInfoManager", "Response : " + new String(bArr));
            }
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            GuliLog.m12370d("WeightInfoManager", "Sync Deleted WeightInfos To Server Success!!");
            if (bArr != null) {
                String str = new String(bArr);
                GuliLog.tracePrintInfo("WeightInfoManager", "Response : " + str);
                if (WebResponse.getWebResponse(this.f11343a, str).m11659c()) {
                    GuliLog.m12370d("WeightInfoManager", "Result OK!!");
                    this.f11344b.result = true;
                    this.f11346d.deleteInfosLocal(this.f11345c);
                }
            }
        }
    }

    public boolean syncDeletedWeightInfosToServer(Context context, List<WeightInfo> list) { // m15767b
        GuliLog.tracePrintInfo(TAG, "Sync Deleted WeightInfos To Server!!");
        if (list == null || list.isEmpty()) {
            return true;
        }
        String g = m15735g((List) list);
        SyncResult syncResult = new SyncResult();
        // TBD sync to server
        WebAPI.syncDeletedWeightInfosToServer(C0886a.m9071f(context), g, new SyncDeletedWeightInfosToServerHandler(this, context, syncResult, list));
        return syncResult.result;

    }

    public List<WeightInfo> getAllInfos() { // m15768c
        List<WeightInfo> d = new Select().from(WeightInfo.class).orderBy("Timestamp DESC").execute();
        GuliLog.tracePrintInfo(TAG, "Get All Infos : " + d.size());
        for (WeightInfo weightInfo : d) {
            GuliLog.tracePrintInfo(TAG, "Get Info : " + weightInfo);
        }
        return d;
    }

    public List<WeightInfo> m15769c(int i) {
        From k = getInfoNeedToDelete();
        if (i > 0) {
            k = k.limit(i);
        }
        List<WeightInfo> d = k.execute();
        GuliLog.tracePrintInfo(TAG, "Get Infos Deleted : " + d.size());
        return d;
    }

    public void m15770c(WeightInfo weightInfo) {
        if (weightInfo.synced == 0) {
            deleteInfoLocal(weightInfo);
        } else {
            deleteInfoMark(weightInfo);
        }
    }

    public boolean deleteInfosLocal(List<WeightInfo> list) { // m15771c
        GuliLog.m12370d(TAG, "Delete Infos Local : " + list.size());
        ActiveAndroid.beginTransaction();
        boolean z;
        try {
            for (WeightInfo d : list) {
                deleteInfoLocal(d);
            }
            ActiveAndroid.setTransactionSuccessful();
            z = true;
            return z;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            ActiveAndroid.endTransaction();
        }
    }

    public List<WeightInfo> m15772d() {
        return m15769c(0);
    }

    public List<WeightInfo> m15773d(int i) {
        From l = getInfoNeedToSync();
        if (i > 0) {
            l = l.limit(i);
        }
        List<WeightInfo> d = l.execute();
        GuliLog.tracePrintInfo(TAG, "Get Infos NeedSync : " + d.size());
        return d;
    }

    public void deleteInfoLocal(WeightInfo weightInfo) { // m15774d
        GuliLog.m12370d(TAG, "Delete Info Local : " + weightInfo);
        weightInfo.delete();
    }

    public boolean updateInfos(List<WeightInfo> list) { // m15775d
        GuliLog.m12370d(TAG, "Update Infos : " + list.size());
        ActiveAndroid.beginTransaction();
        boolean z;
        try {
            for (WeightInfo j : list) {
                m15740j(j);
            }
            ActiveAndroid.setTransactionSuccessful();
            z = true;
            return z;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            ActiveAndroid.endTransaction();
        }
    }

    public int getCountInfoNeedToDelete() { // m15776e
        int g = getInfoNeedToDelete().count();
        GuliLog.tracePrintInfo(TAG, "Get Infos Deleted Count : " + g);
        return g;
    }

    public WeightInfo m15777e(int i) {
        Object[] objArr = new Object[]{Integer.valueOf(i)};
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", objArr).orderBy("Timestamp DESC").executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Latest Info : " + i + ", " + weightInfo);
        return weightInfo;
    }

    public void m15778e(WeightInfo weightInfo) {
        m15764b(weightInfo, true);
    }

    public WeightInfo m15779f(int i) {
        Object[] objArr = new Object[]{Integer.valueOf(i)};
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).and("UserId=?", objArr).orderBy("Timestamp ASC").executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Earliest Info : " + i + ", " + weightInfo);
        return weightInfo;
    }

    public List<WeightInfo> m15780f() {
        return m15773d(0);
    }

    public int getCountInfoNeedToSync() { // m15781g
        int g = getInfoNeedToSync().count();
        GuliLog.tracePrintInfo(TAG, "Get Infos NeedSync Count : " + g);
        return g;
    }

    public WeightInfo m15782h() {
        return (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).orderBy("Timestamp DESC").executeSingle();
    }

    public SportDay m15783i() {
        SportDay sportDay = new SportDay();
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).orderBy("Timestamp ASC").executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Oldest Info : " + weightInfo);
        if (weightInfo == null) {
            return sportDay;
        }
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(weightInfo.timestamp);
        return new SportDay(instance);
    }

    public SportDay m15784j() {
        SportDay sportDay = new SportDay();
        WeightInfo weightInfo = (WeightInfo) new Select().from(WeightInfo.class).where("Synced>?", Integer.valueOf(-1)).orderBy("Timestamp DESC").executeSingle();
        GuliLog.tracePrintInfo(TAG, "Get Latest Info : " + weightInfo);
        if (weightInfo == null) {
            return sportDay;
        }
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(weightInfo.timestamp);
        return new SportDay(instance);
    }
}
